#!/usr/bin/env python3

import sys
import os
import re
import time
import stat
import sysbench_mysql
import sysbench_vdbench
import stat_result 
import stat_result_vdbench 

counter = 0
start = time.perf_counter()
total_length = 2 
totol_files = 1

def do_the_test(path, delay):
    global counter
    for root, dirs, files in os.walk(path, topdown=True):
        if  dirs:
            print("-------dir:", root)
        for name in files:

            fname = os.path.join(root, name)
            
            permission = os.stat(fname)
            if not permission.st_mode & stat.S_IRUSR:
                continue
            if not permission.st_mode & stat.S_IWUSR:
                continue

            fin = open(fname, "r+")
 
            try:
                val = fin.read()
            except OSError:
                fin.close()
                continue
            number = int(val)
            try:
                number = int(val)
            except ValueError:
                fin.close()
                continue
            if number <= 1:
                fin.close()
                continue
            delta = int(number * 5 / 10)
            multi = 1
            if delta >> 7 > 10:
                multi = 100
            elif delta >> 4 > 10:
                multi = 10
            elif delta >> 2 > 10:
                multi = 4
            number = int(number / multi)
            delta = int(delta / multi)
            little = number - delta
            big = number + delta
            for i in range(little, big):
                tmp_val = i * multi
                try:
                    fin.seek(0, 0)
                    fin.write(str(tmp_val))
                except OSError:
                    break
                try:
                    fin.seek(0, 0)
                    sysctl_value = fname + " value: "+ fin.read()
                except OSError:
                   break
                #do the test 
                sysbench_mysql.do_the_test(0, int(delay), "mysql", sysctl_value)
                sysbench_vdbench.do_the_test("vdbench", sysctl_value, str(delay))
                #stat the result
                stat_result.do_the_stat()
                stat_result_vdbench.do_the_stat(str(delay))

            fin.seek(0, 0)
            fin.write(val)
            fin.seek(0, 0)
            print("\nfile:"+fname +" default value: "+fin.read() + "\n")
            fin.close()
            counter += 1
            show_progress()
        #for name in dirs:
        #    print("-------dir:", name)
    return


def count_files_in_directory(directory, topdown=True):
    total_files = 0
    for root, dirs, files in os.walk(directory):
        for fname in files:
            total_files += 1
    return total_files


def show_progress():
    processed = int(float(counter / totol_files) * total_length)
    unprocess = total_length - processed
    a = "*" * processed
    b = "." * unprocess
    c = (counter / totol_files) * 100
    dur = time.perf_counter() - start
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end = "")

def scan_dir(path, delay):
    global totol_files
    counter = 0
    print("------Start to scan file in {path}------\n")
    totol_files = count_files_in_directory(path) * 2
    do_the_test(path, delay)
    print(f"\n------Scan file in {path} finished.------\n")


#if __name__ == "__main__":
#    path = sys.argv[1]
#    os.chdir(path)
#    print(f"Start to scan file in {path}\n")
#    totol_files = count_files_in_directory(path) * 2
#    change_file(path)
#    print(f"\nScan file in {path} finished.\n")
